package chen.web.controller.forum;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import chen.web.controller.ResourceNotFoundException;
import chen.web.forum.Category;
import chen.web.forum.CategoryManager;
import chen.web.forum.Reply;
import chen.web.forum.ReplyCondition;
import chen.web.forum.Subject;
import chen.web.forum.SubjectCondition;
import chen.web.forum.TopicInfo;
import chen.web.forum.TopicManager;
import chen.web.forum.SubjectCondition.Order;
import chen.web.forum.SubjectCondition.OrderBy;
import chen.web.util.Pagination;


public class RecyleBinAndCheckingController {

	private static final int Topic_Info_Count_Per_Page = 20;
	
	private @Autowired TopicManager tm;
	private @Autowired CategoryManager cm;
	
	private Category getCategory(String path){
		Category category = cm.getByPath("/"+path);
		if(category == null){
			throw new ResourceNotFoundException();
		}
		return category;
	}
	
	//回收站的主题信息，path为栏目路径
	@RequestMapping(value="/recycle-bin/topic/{path}", method=RequestMethod.GET)
	public @ResponseBody JSONResponse listDeletedTopicInfo(@PathVariable String path){
		return listTopicInfo(path, Type.deleted);
	}
	
	//未审核的主题信息，path为栏目路径
	@RequestMapping(value="/checking/topic/{path}", method=RequestMethod.GET)
	public @ResponseBody JSONResponse listUncheckedTopicInfo(@PathVariable String path){
		return listTopicInfo(path, Type.unchecked);
	}
	
	private static enum Type {deleted, unchecked};
	
	private JSONResponse listTopicInfo(String path, Type type){
		Category category = getCategory(path);
		SubjectCondition cond = new SubjectCondition().setCategory(category).setContainsChildren(true);
		if(type == Type.deleted){
			cond.setFakeDelete(true);
		} else if(type == Type.unchecked){
			cond.setNeedChecking(true);
		} else throw new RuntimeException();
		
		int itemAmount = tm.totalSubject(cond);
		Pagination pagination = new Pagination(itemAmount, Topic_Info_Count_Per_Page);
		List<TopicInfo> topicInfoList = tm.getTopicInfoList(cond.setOrderBy(OrderBy.LastReplyDate).setOrder(Order.Desc), pagination.getBeginOffset(), pagination.getPageInfoCount());

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("topicInfoList", topicInfoList);
		map.put("pagination", pagination);
		return JSONResponse.successed(map);
	}
	
	//还原回收站主题
	@RequestMapping(value="/recycle-bin/topic/{ids}/restore", method=RequestMethod.POST)
	public @ResponseBody JSONResponse restoreTopic(@PathVariable String ids){
		String[] idArr= ids.split("_");
		for(String id : idArr){
			Subject subject = tm.getSubject(Integer.parseInt(id));
			tm.restore(subject);
		}
		return JSONResponse.successed();
	}
	
	//回收站的回复信息，path为栏目路径
	@RequestMapping(value="/recycle-bin/reply/{path}", method=RequestMethod.GET)
	public @ResponseBody JSONResponse listDeletedReply(@PathVariable String path){
		return listReply(path, Type.deleted);
	}
	
	//回收站的回复信息，path为栏目路径
	@RequestMapping(value="/checking/reply/{path}", method=RequestMethod.GET)
	public @ResponseBody JSONResponse listUncheckedReply(@PathVariable String path){
		return listReply(path, Type.unchecked);
	}
	
	private JSONResponse listReply(String path, Type type){
		Category category = getCategory(path);
		ReplyCondition cond = new ReplyCondition().setCategory(category);
		if(type == Type.deleted){
			cond.setFakeDelete(true);
		} else if(type == Type.unchecked){
			cond.setNeedChecking(true);
		} else throw new RuntimeException();
		
		int itemAmount = tm.totalReply(cond);
		Pagination pagination = new Pagination(itemAmount, Topic_Info_Count_Per_Page);
		List<Reply> replyList = tm.getReplyList(cond, pagination.getBeginOffset(), pagination.getPageInfoCount());

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("replyList", replyList);
		map.put("pagination", pagination);
		
		return JSONResponse.successed(map);
	}
	
	//还原回收站回复
	@RequestMapping(value="/recycle-bin/reply/{ids}/restore", method=RequestMethod.POST)
	public @ResponseBody JSONResponse restoreReply(@PathVariable String ids){
		String[] idArr= ids.split("_");
		for(String id : idArr){
			Reply reply = tm.getReply(Integer.parseInt(id));
			tm.restore(reply);
		}
		return JSONResponse.successed();
	}
	
	@RequestMapping(value="/recycle-bin/reply/{ids}/delete", method=RequestMethod.POST)
	public @ResponseBody JSONResponse deleteReply(@PathVariable String ids){
		String[] idArr= ids.split("_");
		for(String id : idArr){
			Reply reply = tm.getReply(Integer.parseInt(id));
			tm.delete(reply);
		}
		return JSONResponse.successed();
	}
	
	@RequestMapping(value="/recycle-bin/topic/{ids}/delete", method=RequestMethod.POST)
	public @ResponseBody JSONResponse deleteTopic(@PathVariable String ids){
		String[] idArr= ids.split("_");
		for(String id : idArr){
			Subject subject = tm.getSubject(Integer.parseInt(id));
			tm.delete(subject);
		}
		return JSONResponse.successed();
	}
}
